/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
<template>
    <m-popover ref="popover" :width="400" :ok-text="item ? $t('Edit') : $t('Submit')" @ok="_ok" @close="close">
        <template slot="content">
            <div class="create-tenement-model">
                <el-form ref="form" :model="form" :rules="rules" label-width="100px" label-position="right">
                    <el-form-item label="角色名称" prop="roleName">
                        <el-input type="input"  v-model="form.roleName" maxlength="60" size="small"
                                  placeholder='请输入角色名称'>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="角色描述" prop="roleComment">
                        <el-input type="textarea"  v-model="form.roleComment" maxlength="50" size="small"
                                  placeholder='请输入角色描述 ' show-word-limit>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="所属租户" prop="tenantId">
                        <el-select v-model="form.tenantId" size="small" style="width: 100%;">
                            <el-option v-for="city in queueList" :key="city.id" :value="city.id" :label="city.code"
                                       placeholder='请选择租户'>
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
        </template>
    </m-popover>
</template>
<script>
  import _ from 'lodash'
  import store from '@/conf/home/store'
  import mPopover from '@/module/components/popup/popover'
  export default {
    name: 'create-tenement',
    data () {
      return {
        store,
        queueList: [],
        form: {
          roleName: '',
          roleComment: '',
          tenantId: ''
        },
        rules: {
          roleName: [{ required: true, message: '请输入角色名称' }],
          roleComment: [{ required: true, message: '请输入角色描述' }],
          tenantId: [{ required: true, message: '请选择租户' }]
        }
      }
    },
    props: {
      item: Object
    },
    methods: {
      _ok () {
        this.$refs.form.validate(valid => {
          if (valid) {
            this._submit()
          }
        })
      },
      _getTenantList () {
        return new Promise((resolve, reject) => {
          this.store.dispatch('security/getTenantList').then(res => {
            // console.log(res)
            this.queueList = _.map(res, v => {
              return {
                id: v.id,
                code: v.tenantCode
              }
            }).filter(e => e.id !== -1)
            this.$nextTick(() => {
              this.queueId = this.queueList[0].id
            })
            resolve()
          })
        })
      },
      _submit () {
        // 提交
        let param = {
          tenantId: this.form.tenantId,
          roleName: this.form.roleName,
          roleComment: this.form.roleComment
        }
        if (this.item) {
          param.id = this.item.id
        }
        this.$refs.popover.spinnerLoading = true
        this.store.dispatch(`permission/${this.item ? 'updateRole' : 'createRole'}`, param).then(res => {
          this.$emit('onUpdate')
          this.$message.success(res.msg)
          this.$refs.popover.spinnerLoading = false
        }).catch(e => {
          this.$message.error(e.msg || '')
          this.$refs.popover.spinnerLoading = false
        })
      },
      close () {
        this.$emit('close')
      }
    },
    watch: {
    },
    created () {
      this._getTenantList().then(res => {
        if (this.item) {
          this.$nextTick(() => {
            this.form.tenantId = this.item.tenantId
          })
          this.form.roleName = this.item.roleName
          this.form.roleComment = this.item.roleComment
        }
      })
    },
    mounted () {
    },
    components: { mPopover }
  }
</script>
